package com.ccb.sc.common.modules.shopkeeper.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.common.modules.shopkeeper.convert.ShopkeeperGoodsConvert;
import com.ccb.sc.common.modules.shopkeeper.entity.ShopkeeperGoods;
import com.ccb.sc.common.modules.shopkeeper.entity.ShopkeeperGoodsCarousel;
import com.ccb.sc.common.modules.shopkeeper.entity.ShopkeeperGoodsSpecs;
import com.ccb.sc.common.modules.shopkeeper.mapper.ShopkeeperGoodsCarouselMapper;
import com.ccb.sc.common.modules.shopkeeper.mapper.ShopkeeperGoodsMapper;
import com.ccb.sc.common.modules.shopkeeper.mapper.ShopkeeperGoodsSpecsMapper;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperGoodsDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ken
 * @version 1.0
 * @date 2023/2/18 22:59
 */
@Service
public class ShopkeeperGoodsService extends ServiceImpl<ShopkeeperGoodsMapper, ShopkeeperGoods> {

    private ShopkeeperGoodsCarouselMapper goodsCarouselMapper;
    private ShopkeeperGoodsSpecsMapper goodsSpecsMapper;
    /*private ISettingsService settingsService;*/

    public ShopkeeperGoodsDetailVo detail(Long goodsId) {
        ShopkeeperGoodsDetailVo vo = this.getBaseMapper().selectDetail(goodsId);
        List<ShopkeeperGoodsCarousel> carousels = this.getGoodsCarousels(goodsId);
        if (!CollectionUtils.isEmpty(carousels)) {
            vo.setCarousels(carousels.stream().map(ShopkeeperGoodsCarousel::getUrl).collect(Collectors.toList()));
        }
        List<ShopkeeperGoodsSpecs> specs = this.getGoodsSpecsByGoodsId(goodsId);
        if (!CollectionUtils.isEmpty(specs)) {
            vo.setSpecs(ShopkeeperGoodsConvert.INSTANCE.goodsSpec2VoBatch(specs));
        }
        /*ShopkeeperAwardSettingsVo settings = this.settingsService.getSettings(ShopkeeperAwardSettingsVo.class);
        BigDecimal maxAward = settings.getLevel3DirectSuperiorAward();*/

        return vo;
    }

    public List<ShopkeeperGoodsCarousel> getGoodsCarousels(Long goodsId) {
        return this.goodsCarouselMapper.selectList(new QueryWrapper<ShopkeeperGoodsCarousel>().lambda().eq(ShopkeeperGoodsCarousel::getGoodsId, goodsId));
    }

    public List<ShopkeeperGoodsSpecs> getGoodsSpecsByGoodsId(Long goodsId) {
        return this.goodsSpecsMapper.selectList(new QueryWrapper<ShopkeeperGoodsSpecs>().lambda().eq(ShopkeeperGoodsSpecs::getGoodsId, goodsId));
    }

    @Autowired
    public void setGoodsCarouselMapper(ShopkeeperGoodsCarouselMapper goodsCarouselMapper) {
        this.goodsCarouselMapper = goodsCarouselMapper;
    }

    @Autowired
    public void setGoodsSpecsMapper(ShopkeeperGoodsSpecsMapper goodsSpecsMapper) {
        this.goodsSpecsMapper = goodsSpecsMapper;
    }

    /*@Autowired
    public void setSettingsService(ISettingsService settingsService) {
        this.settingsService = settingsService;
    }*/
}
